﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IRProject.Model;

namespace IRProject
{
    public partial class Form1 : Form
    {
        Model.Model model;
        ProgressForm pForm;
        public Form1()
        {
            InitializeComponent();
            pForm = new ProgressForm();
            textBox1.Enabled = false;
            searchButton.Enabled = false;
            cleanButton.Enabled = false;
            //test
            //List<ResultData> results = new List<ResultData>();
            //results.Add(new ResultData() { DocId = 1, Similarity = 0.5f });
            //results.Add(new ResultData() { DocId = 15, Similarity = 0.051f });
            //updateResult(results);
            //test
        }

        void InitializeModel()
        {
            model = new Model.Model();
            model.OnProgressChangeEvent += this.onProgressChangeEvent;
            model.OnUpdateResultEvent += this.updateResult;
            textBox1.Enabled = true;
            searchButton.Enabled = true;
        }

        void onProgressChangeEvent(object sender, ProgressEventArgs e)
        {
            if (e.State == EProgressState.Start)
            {
                pForm.Show();
                pForm.Message = e.Message;
            }
            if (e.State == EProgressState.Progressing)
            {
                pForm.Show();
                pForm.Progress = e.Progress;
            }
            if (e.State == EProgressState.End)
                pForm.Hide();
        }

        void updateResult(object result)
        {
            resultGridView.DataSource = result;
            if (result != null)
                cleanButton.Enabled = true;
            else
                cleanButton.Enabled = false;
        }

        private void openFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.warc)|*.warc|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                InitializeModel();
                string filePath = openFileDialog1.FileName;
                model.Load(filePath);
                model.AnalysisDocs();
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            model.InputQuery(textBox1.Text);
            updateResult(model.ResultDataList);
        }

        private void resultGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridView v = sender as DataGridView;
            List<ResultData> datas = v.DataSource as List<ResultData>;
            ResultData selected = datas[e.RowIndex];
            string contain = model.Documents[selected.DocId];
            //MessageBox.Show(contain);

            using (TextView tv = new TextView(contain))
            {
                tv.ShowDialog();
            }

        }

        private void cleanButton_Click(object sender, EventArgs e)
        {
            updateResult(null);
        }


    }
}
